package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;
import com.heima.pojo.TbNode;
import com.heima.pojo.dto.NodeDto;
import com.heima.pojo.vo.NodeVo;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.VendingMachineVo;
import com.heima.service.ITbNodeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 点位表 前端控制器
 * </p>
 *
 * @author nan
 * @since 2024-06-09
 */
@RestController
@RequestMapping("/api/vm-service/node")
public class TbNodeController {

    @Autowired
    private ITbNodeService nodeService;

    @GetMapping("/search")
    @ApiOperation("查看区域详情")
    public PageResponse<NodeVo> getByPage(@RequestParam Integer pageIndex,
                                          @RequestParam Integer pageSize,
                                          @RequestParam(required = false) String regionId,
                                          @RequestParam(required = false) String name) {

        return nodeService.getByPage(pageIndex, pageSize, regionId, name);
    }

    @PostMapping
    @ApiOperation("新增点位")
    public Boolean addNode(@RequestBody NodeDto nodeDto) {
        return nodeService.save(BeanUtil.toBean(nodeDto, TbNode.class));
    }

    @GetMapping("/vmList/{nodeId}")
    @ApiOperation("获取点位详情")
    public List<VendingMachineVo> getByNodeId(@PathVariable String nodeId) {
        return nodeService.getByNodeId(nodeId);
    }

    @PutMapping("/{nodeId}")
    @ApiOperation("修改点位")
    public Boolean updateNode(@PathVariable String nodeId, @RequestBody TbNode node) {
        node.setId(Long.parseLong(nodeId));
        return nodeService.updateById(node);
    }

    @DeleteMapping("/{nodeId}")
    @ApiOperation("删除点位")
    public Boolean deleteNodeById(@PathVariable String nodeId) {
        return nodeService.removeById(nodeId);
    }

    @GetMapping("/count")
    @ApiOperation("点位数量")
    public Integer nodeCount() {
        return nodeService.count();
    }

}
